package com.zz.nondatain.view.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zz.nondatain.R;
import com.zz.nondatain.base.BaseActivity;
import com.zz.nondatain.databinding.ActivityMySignInBinding;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

/**
 * 我的签到页面，显示用户签到记录
 */
public class MySignInActivity extends BaseActivity<ActivityMySignInBinding> {

    private Calendar currentDate;
    private List<SignInRecord> signInRecords = new ArrayList<>();
    private SignInAdapter adapter;

    @Override
    protected void initView() {
        // 初始化日期选择
        currentDate = Calendar.getInstance();
        updateDateDisplay();
        
        // 设置RecyclerView
        binding.recyclerSignInRecords.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SignInAdapter(signInRecords);
        binding.recyclerSignInRecords.setAdapter(adapter);
        
        // 加载当前月份的签到记录
        loadSignInRecords();
    }

    @Override
    protected void initData() {
        // 不需要额外初始化数据
    }

    @Override
    protected void initObservers() {
        // 设置监听器
        setupListeners();
    }

    @Override
    protected ActivityMySignInBinding getViewBinding() {
        return ActivityMySignInBinding.inflate(getLayoutInflater());
    }
    
    /**
     * 设置监听器
     */
    private void setupListeners() {
        // 返回按钮点击事件
        binding.btnBack.setOnClickListener(v -> finish());
        
        // 上个月按钮点击事件
        binding.btnPrevMonth.setOnClickListener(v -> {
            currentDate.add(Calendar.MONTH, -1);
            updateDateDisplay();
            loadSignInRecords();
        });
        
        // 下个月按钮点击事件
        binding.btnNextMonth.setOnClickListener(v -> {
            currentDate.add(Calendar.MONTH, 1);
            updateDateDisplay();
            loadSignInRecords();
        });
    }
    
    /**
     * 更新日期显示
     */
    private void updateDateDisplay() {
        // 更新年月显示
        SimpleDateFormat yearMonthFormat = new SimpleDateFormat("yyyy 年 M 月", Locale.getDefault());
        binding.tvYearMonth.setText(yearMonthFormat.format(currentDate.getTime()));
        
        // 更新月份选择器显示
        SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM", Locale.getDefault());
        binding.tvCurrentMonth.setText(monthFormat.format(currentDate.getTime()));
    }
    
    /**
     * 加载签到记录
     */
    private void loadSignInRecords() {
        // 清空现有记录
        signInRecords.clear();
        
        // 获取当前年月
        int year = currentDate.get(Calendar.YEAR);
        int month = currentDate.get(Calendar.MONTH) + 1; // Calendar月份从0开始
        
        // 模拟数据 - 实际应该从API或数据库获取
        if (year == 2024 && month == 11) {
            // 2024年11月有签到记录
            signInRecords.add(new SignInRecord("2024-11-19 11:40:38", 
                    "中国成都市武侯区武兴五路433号2栋西部智谷A区", "忘记打卡"));
            signInRecords.add(new SignInRecord("2024-11-19 13:58:56", 
                    "中国成都市武侯区武兴五路433号2栋西部智谷A区", "请假"));
            signInRecords.add(new SignInRecord("2024-11-19 14:54:14", 
                    "中国成都市武侯区武兴五路433号2栋西部智谷A区", "要求主动签到"));
            signInRecords.add(new SignInRecord("2024-11-19 14:57:49", 
                    "中国成都市武侯区智远大道433号2栋西部智谷A区", "要求主动签到"));
            
            // 设置签到次数
            binding.tvSignInCount.setText("本月累计签到15次");
            
            // 显示签到列表，隐藏暂无数据
            binding.recyclerSignInRecords.setVisibility(View.VISIBLE);
            binding.layoutNoData.setVisibility(View.GONE);
        } else if (year == 2025 && month == 4) {
            // 2025年4月暂无签到记录
            binding.tvSignInCount.setText("本月累计签到0次");
            
            // 隐藏签到列表，显示暂无数据
            binding.recyclerSignInRecords.setVisibility(View.GONE);
            binding.layoutNoData.setVisibility(View.VISIBLE);
        } else {
            // 其他月份暂无签到记录
            binding.tvSignInCount.setText("本月累计签到0次");
            
            // 隐藏签到列表，显示暂无数据
            binding.recyclerSignInRecords.setVisibility(View.GONE);
            binding.layoutNoData.setVisibility(View.VISIBLE);
        }
        
        // 刷新适配器
        adapter.notifyDataSetChanged();
    }
    
    /**
     * 签到记录数据类
     */
    private static class SignInRecord {
        String time;
        String location;
        String reason;
        
        SignInRecord(String time, String location, String reason) {
            this.time = time;
            this.location = location;
            this.reason = reason;
        }
    }
    
    /**
     * 签到记录适配器
     */
    private class SignInAdapter extends RecyclerView.Adapter<SignInAdapter.ViewHolder> {
        
        private List<SignInRecord> records;
        
        SignInAdapter(List<SignInRecord> records) {
            this.records = records;
        }
        
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_sign_in_record, parent, false);
            return new ViewHolder(view);
        }
        
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            SignInRecord record = records.get(position);
            holder.tvTime.setText(record.time);
            holder.tvLocation.setText(record.location);
            holder.tvReason.setText(record.reason);
        }
        
        @Override
        public int getItemCount() {
            return records.size();
        }
        
        class ViewHolder extends RecyclerView.ViewHolder {
            TextView tvTime;
            TextView tvLocation;
            TextView tvReason;
            
            ViewHolder(View itemView) {
                super(itemView);
                tvTime = itemView.findViewById(R.id.tv_sign_in_time);
                tvLocation = itemView.findViewById(R.id.tv_sign_in_location);
                tvReason = itemView.findViewById(R.id.tv_sign_in_reason);
            }
        }
    }
} 